
var that
var app = getApp()
Page({
  data: {
    ordersn:"njdjhiulf123978",
    price:9,
    orderid:'',

    showModalStatus: false,
    pwd:'',  //输入的密码
    over_money:999,  //账户余额
  },
  onLoad: function (options) {
    that=this;
    that.setData({
      orderid: options.orderid, over_money: app.data.person_msg_rr.credit2})
    that.get_shop_order();
  },
  onReady: function () {
  },
  onShow: function () {
  },
  cle: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  input_pwd: function (e) {
    that.setData({ pwd: e.detail.value.trim() })
    if (that.data.pwd.length == 6){//密码位数够了就判断
      app.req("pay_from_member.html", { "ordersn": that.data.ordersn, "price": that.data.price, "pwd": that.data.pwd, "type": 'shop', "openid": app.data.openid,'shop_id':wx.getStorageSync('shop_id')}, function (res) {
        if (res.status == "success") {
          app.toast_up(res.mess)
          that.util('close')//关闭支付弹窗

            //支付成功传第订单号进入订单详情
        } else if (res.status == "error") {
          app.toast_up(res.mess)
          that.setData({ pwd: '' })
        } else {
          app.modal("错误提示", "网络错误，是否重试？", function (re) {
            if (re) {
              that.input_pwd(e)
            }
          })
        }
      })
    }
  },
  wx_pay:function(e){
    if (app.data.person_msg_rr.level>0){  //如果是会员，就会员支付
      var currentStatu = e.currentTarget.dataset.statu;
      this.util(currentStatu)
      return
    }
    app.req("pay_shop.html", { "openid": app.data.openid, 'price': that.data.price, 'ordersn': that.data.ordersn ,'shop_id':wx.getStorageSync('shop_id')}, function (res) {
      if (res.status == "success") {
        wx.requestPayment(
          {
            'timeStamp': res.mess.timeStamp,
            'nonceStr': res.mess.nonceStr,
            'package': res.mess.package,
            'signType': res.mess.signType,
            'paySign': res.mess.paySign,
            'success': function (re) {
              app.toast("支付成功！")
              //支付成功传第订单号进入订单详情
            },
            'fail': function (re) {
              app.toast_up("支付失败！")
            },
            'complete': function (res) { }
          })
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.wx_pay(e)
          }
        })
      }
    })
  },
  get_shop_order:function(){
    app.req("get_shop_order.html", { "orderid": that.data.orderid }, function (res) {
      if (res.status == "success") {
        console.log(res)
        that.setData({ ordersn: res.mess.ordersn, price: res.mess.price})
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_shop_order()
          }
        })
      }
    })
  },

  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });

    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation;

    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function () {
      // 执行第二组动画 
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)
    // 显示 
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  } 

})